package cn.itcast.bos.service.transit.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.WayBillRepository;
import cn.itcast.bos.dao.transit.TransitInfoRepository;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.service.transit.TransitInfoService;

@Service
@Transactional
public class TransitInfoServiceImpl implements TransitInfoService {
	
	@Autowired
	private WayBillRepository wayBillRepository;
	private static Logger logger = Logger.getLogger(TransitInfo.class);
	@Autowired
	private TransitInfoRepository TransitInfoRepository;

	@Override
	public void saveTransitInfo(String ids) {
		if(StringUtils.isNotBlank(ids) && ids.split(",").length > 0){
			for(String id : ids.split(",")){
				//查询数据库中没有中转的运单
				WayBill wayBill = wayBillRepository.findOne(Integer.parseInt(id));
				if(wayBill != null && wayBill.getSignStatus() == 1){
					TransitInfo transitInfo = new TransitInfo();
					transitInfo.setWayBill(wayBill);
					transitInfo.setStatus("出入库中转");
					TransitInfoRepository.save(transitInfo);
					logger.info(wayBill.getRecAddress()+"运单开始流转");
					wayBill.setSignStatus(2);//运单派送中
				}
			}
		}
		
	}

	@Override
	public Page<TransitInfo> findPageQuery(Pageable pages) {
		return TransitInfoRepository.findAll(pages);
	}

}
